package com.nut.course.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nut.course.api.req.CourseNum;
import com.nut.course.api.req.CourseOrderReq;
import com.nut.course.common.Constants;
import com.nut.course.common.Result;
import com.nut.course.entity.Course;
import com.nut.course.entity.CourseOrder;
import com.nut.course.entity.PartnerCourse;
import com.nut.course.enums.CourseStatusEnum;
import com.nut.course.interceptor.UserJwt;
import com.nut.course.mapper.BizCourseMapper;
import com.nut.course.mapper.BizCourseOrderMapper;
import com.nut.course.mapper.BizPartnerCourseMapper;
import com.nut.course.service.CourseOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Set;

@Slf4j
@Service
public class CourseOrderServiceImpl implements CourseOrderService {

    @Autowired
    private BizCourseMapper bizCourseMapper;

    @Autowired
    private BizCourseOrderMapper bizCourseOrderMapper;

    @Autowired
    private BizPartnerCourseMapper bizPartnerCourseMapper;

    @Override
    public Result<IPage<CourseOrder>> list(Integer schoolId, Integer partnerId, String status, String name, Integer pageNo, Integer pageSize) {
        Page<CourseOrder> page = new Page<>();
        page.setSize(pageSize);
        page.setCurrent(pageNo);

        String[] statusIds = null;
        if (StringUtils.hasLength(status)) {
            statusIds = status.split(",");
        }

        List<CourseOrder> list = bizCourseOrderMapper.selectListPage(page, schoolId, partnerId, statusIds, name);
        page.setRecords(list);
        return Result.ok(page);
    }

    @Override
    public Result<CourseOrder> getById(Integer id) {
        CourseOrder course = bizCourseOrderMapper.selectById(id);
        return Result.ok(course);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result submit(UserJwt userJwt, CourseOrderReq courseOrderReq) {

        List<CourseNum> courses = courseOrderReq.getCourses();

        for (CourseNum courseNum : courses) {

            Integer partnerCourseId = courseNum.getCourseId();

            PartnerCourse partnerCourse = bizPartnerCourseMapper.selectById(partnerCourseId);
            if (partnerCourse == null) {
                log.warn("not_find_partnerCourse,id=" + partnerCourseId);
                continue;
            }

            //TODO  生成课程订单
            Course course = new Course();
            course.setUserId(userJwt.getUserId());
            course.setName(partnerCourse.getName());
            course.setType(partnerCourse.getType());
            //2外聘课程
            course.setNature(2);
            course.setNum(partnerCourse.getNum());
            course.setGradeStart(partnerCourse.getGradeStart());
            course.setGradeEnd(partnerCourse.getGradeEnd());
            course.setContent(partnerCourse.getDetail());
            //课程为待审核状态
            course.setStatus(CourseStatusEnum.CHECK_WAIT_1.getValue());
            course.setSubmitTime(new Date());
//            course.setAddress(null);
//            course.setTime(null);
//            course.setPlan(null);
//            course.setTeacher(null);
//            course.setManager(null);

            bizCourseMapper.insert(course);

            CourseOrder order = new CourseOrder();
            order.setCourseId(course.getId());
            order.setPartnerCourseId(partnerCourseId);
            order.setName(partnerCourse.getName());
            order.setNum(courseNum.getNum());
            order.setUnitPrice(partnerCourse.getPrice());
            order.setPartnerId(partnerCourse.getPartnerId());
            order.setSchoolId(Constants.DEFAULT_SCHOOL_ID);
            order.setTotalPrice(courseNum.getNum() * partnerCourse.getPrice());
            order.setStatus(1);

            bizCourseOrderMapper.insert(order);

        }

        return Result.ok();
    }

    @Override
    public Result updateById(CourseOrder course) {
        bizCourseOrderMapper.updateById(course);
        return Result.ok();
    }

    @Override
    public Result deleteByIds(Set<Integer> ids) {
        bizCourseOrderMapper.deleteBatchIds(ids);
        return Result.ok();
    }

    public static void main(String[] args) {

        String dd = "1,2,3";

        String[] split = dd.split(",");
        System.out.println(split);

        dd = "1";

        String[] split2 = dd.split(",");
        System.out.println(split2);

    }

}
